<template>
	<el-upload
		class="avatar-uploader"
		:action="baseURL + uploadImgUrl"
		:show-file-list="false"
		:on-success="handleAvatarSuccess"
		:before-upload="beforeAvatarUpload"
		:headers="headers"
		:limit="limit"
		:on-exceed="handleExceed"
		:disabled="disabled"
	>
		<img v-if="url" :src="url" class="avatar" />
		<el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
	</el-upload>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
import { Plus } from '@element-plus/icons-vue';
import { Session } from '/@/utils/storage';
// import type { UploadFile } from 'element-plus';
import { useMessage } from '/@/hooks/message';
const uploadImgUrl = ref('/admin/sys-file/upload'); // 上传的图片服务器地址

const fileSize = ref(0);

const headers = computed(() => {
	return {
		Authorization: 'Bearer ' + Session.get('token'),
	};
});

const url = defineModel('url', { type: String });

// console.log(8888, url);
const props = defineProps({
	// 图片数量限制
	limit: {
		type: Number,
		default: 10,
	},
	// 大小限制(MB)
	fileSizew: {
		type: Number,
		default: 100,
	},
	// 文件类型, 例如['png', 'jpg', 'jpeg']
	fileType: {
		type: Array,
		default: () => ['png', 'jpg', 'jpeg'],
	},
	disabled: {
		type: Boolean,
		default: false,
	},
});

// 文件个数超出
function handleExceed() {
	// proxy.$modal.msgError(`上传文件数量不能超过 ${props.limit} 个!`);
	useMessage().error(`上传文件数量不能超过 ${props.limit} 个!`);
}

const handleAvatarSuccess = (response: any) => {
	url.value = response.data.ossUrl;
};

// 上传前loading加载
function beforeAvatarUpload(file: any) {
	let isImg = false;
	if (props.fileType.length) {
		let fileExtension = '';
		if (file.name.lastIndexOf('.') > -1) {
			fileExtension = file.name.slice(file.name.lastIndexOf('.') + 1);
		}
		isImg = props.fileType.some((type) => {
			if (file.type.indexOf(type) > -1) return true;
			if (fileExtension && fileExtension.indexOf(type) > -1) return true;
			return false;
		});
	} else {
		isImg = file.type.indexOf('image') > -1;
	}
	if (!isImg) {
		// proxy.$modal.msgError(`文件格式不正确, 请上传${props.fileType.join('/')}图片格式文件!`);
		useMessage().error(`文件格式不正确, 请上传${props.fileType.join('/')}图片格式文件!`);
		return false;
	}

	if (props.fileSizew) {
		const isLt = fileSize.value / 1024 / 1024 < props.fileSizew;

		if (!isLt) {
			// proxy.$modal.msgError(`上传头像图片大小不能超过 ${props.fileSize} MB!`);
			useMessage().error(`上传头像图片大小不能超过 ${props.fileSizew} MB!`);
			return false;
		}
	}
}
</script>
<style scoped>
.avatar-uploader {
	width: 100%;
	height: 100%;
	display: flex;
	justify-content: center;
	align-items: center;
}
.avatar-uploader .avatar {
	width: 100%;
	height: 100%;
	display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
	width: 100%;
	height: 100%;
	border: 1px dashed var(--el-border-color);
	border-radius: 6px;
	cursor: pointer;
	position: relative;
	overflow: hidden;
	transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
	border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
	font-size: 28px;
	color: #8c939d;
	width: 100%;
	height: 100%;
	text-align: center;
}
.box_up {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
}
</style>
